package com.jhc.auth.service;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.jhc.PwdUtil;
import com.jhc.api.constant.Constant;
import com.jhc.api.domain.User;
import com.jhc.api.auth.form.LoginBody;
import com.jhc.api.auth.form.RegisterBody;
import com.jhc.api.auth.service.TokenService;
import com.jhc.api.system.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

@Service
@DubboService
public class TokenServiceImpl implements TokenService {
    @DubboReference
    private UserService userService;

    @Override
    public SaTokenInfo login(LoginBody body) {
        User user = userService.login(body.getPhone(), body.getPassword());
        StpUtil.login(user.getId());
        StpUtil.getSession().set(Constant.LOGIN_USER,user);
        return StpUtil.getTokenInfo();
    }

    @Override
    public void register(RegisterBody registerBody) {
        User user = new User()
                .setPhone(registerBody.getPhone())
                .setPassword(PwdUtil.encodePassword(registerBody.getPassword()));
        userService.save(user);
    }
}
